/*

 * Copyright (c) 2017-2018, FastDev 刘强 (fastdev@163.com).

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package of.hotswapinject.core.common;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Util {
    public static final String sep = "\1";
    public static final String optionValueSep = ":";

    public static void saveFile(String path, byte[] classfileBuffer) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            fos.write(classfileBuffer);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (RuntimeException e) {
            e.printStackTrace();
        }
        finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void error(Object msg) {
        Logger.getLogger(String.valueOf(Thread.currentThread().getId())).error(msg);
    }

    public static void trace(Object msg) {
        Logger.getLogger(String.valueOf(Thread.currentThread().getId())).info(msg);
    }

    public static Object getFieldValue(Object instance, Class declareClass, String fieldName) {
        try {
            Field field = declareClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(instance);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static String toJSONString(Object obj) {
        if(obj == null) {
            return null;
        }
        return JSON.toJSONString(obj);
    }

    public static String concat(String[] arr) {
        return String.join(sep, arr);
    }

    public static void saveFile(String path, String content) throws IOException {
        FileOutputStream fs = new FileOutputStream(path);
        try {
            byte[] bytes = content.getBytes();
            fs.write(bytes, 0, bytes.length);
        }
        finally {
            fs.close();
        }
    }

    public static boolean isAbstract(Class cl) {
        return (cl.getModifiers() & Modifier.ABSTRACT) > 0;
    }
}
